/**
 * Created by xc on 2018/03/24.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var async = require( 'async'); // 同步控制

//--------------------------------------------------------------------------------------
// Schema 结构
var YjxTradeOdrDtlVipModel = function(obj){
    YjxBaseModel.call(this,obj);

  //`detail_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '订单明细id',
  //`detail_no` varchar(50) NOT NULL DEFAULT '' COMMENT 'itb自定义子订单号',
  //`order_id` bigint(20) NOT NULL COMMENT '订单id',
  //`uni_item_id` bigint(20) NOT NULL COMMENT '统一项目id（vip商品id）',
  //`price_id` bigint(20) NOT NULL COMMENT '计量价格id（套餐id）',
  //`qty` bigint(20) NOT NULL DEFAULT '0' COMMENT '数量',
  //`unit` varchar(20) NOT NULL DEFAULT '月' COMMENT '计量单位',
  //`pay_type` int(2) NOT NULL DEFAULT '1' COMMENT '支付方式：1现金支付，2积分支付',
  //`fee` bigint(12) NOT NULL DEFAULT '0' COMMENT '订单明细金额（单位为rmb分）',
  //`cost_pts` bigint(12) NOT NULL DEFAULT '0' COMMENT '订单明细积分额',
  //`state` int(3) NOT NULL DEFAULT '0' COMMENT 'VIP(子)订单状态:',
  //`desc` varchar(200) DEFAULT '' COMMENT '订单描述',
  //`valid_time` datetime DEFAULT NULL COMMENT '本订单vip有效开始日期',
  //`expiry_time` datetime DEFAULT NULL COMMENT '本订单vip有效截止日期',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL,
  //`del_flg` int(1) NOT NULL DEFAULT '0',
};

YjxTradeOdrDtlVipModel.prototype = new YjxBaseModel();

// 取得表名
YjxTradeOdrDtlVipModel.prototype.getTableName = function(){
    return 'yjx_trade_odr_dtl_vip';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxTradeOdrDtlVipModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'detail_id='+this.detail_id;
};


// 更新vip订单[有效期]信息
// stTime: 有效开始时间
YjxTradeOdrDtlVipModel.prototype.updVipOdrExpTime = function(detailId, userId, stTime, unit, qty, callback) {
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('YjxTradeOdrDtlVipModel.updVipOdrExpTime Err:'+err);
            return callback(err,null);
        }
        // Use the connection

        var sql = " UPDATE yjx.yjx_trade_odr_dtl_vip t, yjx.yjx_trade_order o "+
            " SET t.update_time=sysdate() "+
            " ,t.update_by="+userId+
            " ,t.valid_time='"+stTime+"'";
        if(unit==1) {// 年
            sql += ' ,t.expiry_time=date_add("'+stTime+'", INTERVAL '+Number(qty||'0')+' YEAR) ';
        } else if(unit==2) {// 季度
            sql += ' ,t.expiry_time=date_add("'+stTime+'", INTERVAL '+Number(qty||'0')+' QUARTER) ';
        } else if(unit==4) {// 周
            sql += ' ,t.expiry_time=date_add("'+stTime+'", INTERVAL '+Number(qty||'0')+' WEEK) ';
        } else if(unit==5) {// 天
            sql += ' ,t.expiry_time=date_add("'+stTime+'", INTERVAL '+Number(qty||'0')+' DAY) ';
        } else {// 默认月
            sql += ' ,t.expiry_time=date_add("'+stTime+'", INTERVAL '+Number(qty||'0')+' MONTH) ';
        }
        sql += " WHERE t.del_flg=0 AND t.detail_id="+detailId+
            " AND o.del_flg=0 AND o.order_id=t.order_id AND o.user_id="+userId;

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            if(error) {
                console.log('YjxTradeOdrDtlVipModel.updVipOdrExpTime error:'+error);
                callback(error);
            } else {
                callback(null);
            }
        });
    });
};


//--------------------------------------------------------------------------------------
// 导出
module.exports = YjxTradeOdrDtlVipModel;